local DIY = require "packages/diy_utility/diy_utility"

local xianzhen = fk.CreateSkill{
  name = "ls__xianzhen",
  tags = {"Phase"}
}

Fk:loadTranslationTable{
  ["ls__xianzhen"] = "陷阵",
  ["#ls__xianzhen-prompt"] = "陷阵：请选择目标角色（可以选自己）",
  [":ls__xianzhen"] = "阶段技，你可令一名角色使用一张【杀】，否则你视为对其使用【过河拆桥】；然后你可移去中央区内的一张【闪】，对其造成1点伤害。",
  ["#ls__xianzhen-use"] = "陷阵：请使用一张【杀】",
  ["#ls__xianzhen_damage"] = "你可移去中央区内的一张【闪】，对%dest造成1点伤害",

  ["$ls__xianzhen1"] = "沙场百战惊尉缭，余勇可堪霍骠姚！",
  ["$ls__xianzhen2"] = "一将当先陷靡阵，三军奋威破坚城！",
}

xianzhen:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

xianzhen:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  max_target_num = 2,
  --max_phase_use_time = 1,
  prompt = "#ls__xianzhen-prompt",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected <= player:getMark("@@yanlv_recover")
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(xianzhen.name, Player.HistoryPhase) <= player:getMark("@@yanlv_damage")
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    for _, p in ipairs(effect.tos) do
      if player.dead then break end
      if not p.dead then
        local params = { ---@type AskToUseCardParams
          skill_name = xianzhen.name,
          pattern = "slash",
          prompt = "#ls__xianzhen-use",
          cancelable = true,
          extra_data = {
            bypass_distances = false,
            bypass_times = true,
          }
        }
        local use = room:askToUseCard(p, params)
        if use then
          room:useCard(use)
        else
          room:useVirtualCard("lvshi__dismantlement", nil, player, p, self.name, true)
        end
        local ids = DIY.getCenterCards(room)
        if #ids == 0 then return end
        local jinks = table.filter(ids, function(id)
          return Fk:getCardById(id).trueName == "jink" and room:getCardArea(id) == Card.DiscardPile
        end) 
        if #jinks > 0 and room:askToSkillInvoke(player, { skill_name = xianzhen.name, prompt = "#ls__xianzhen_damage::"..p.id }) then
          local get = room:askToChooseCards(player, {
            target = player,
            max = 1,
            min = 1,
            skill_name = xianzhen.name,
            flag = { card_data = {{xianzhen.name, jinks}} },
            prompt = "#ls__xianzhen_damage::"..p.id,
          })
          if #get > 0 then
            DIY.removeCenterCards(player, get)
            room:damage({
              from = player,
              to = p,
              damage = 1,
              damageType = fk.NormalDamage,
              skillName = xianzhen.name,
            })
          end
        end
      end
    end
  end,
})

return xianzhen